#!/bin/sh

[ -z "$1" ] && echo "Modo de uso: $0 web.xml" >&2  && exit 1

[ -f $1.withoutcasfilters ] || cp $1 $1.withoutcasfilters

echo 'CAS URL (ej https://localhost:8443/cas-server-webapp-3.4.11)':
read CAS_URL

echo 'Server Name (ej http://localhost:8080)':
read SERVER_NAME

echo 'Filter URL (ej /*)'
read FILTER_URL

sed -i "1,/<filter>/ s_<filter>_<!-- CAS -->\\
   <filter>\\
     <filter-name>CAS Authentication Filter</filter-name>\\
     <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>\\
     <init-param>\\
       <param-name>casServerLoginUrl</param-name>\\
       <param-value>$CAS_URL/login</param-value>\\
     </init-param>\\
     <init-param>\\
       <param-name>serverName</param-name>\\
       <param-value>$SERVER_NAME</param-value>\\
     </init-param>\\
   </filter>\\
   <filter>\\
     <filter-name>CAS Validation Filter</filter-name>\\
     <filter-class>org.jasig.cas.client.validation.Cas10TicketValidationFilter</filter-class>\\
     <init-param>\\
       <param-name>casServerUrlPrefix</param-name>\\
       <param-value>$CAS_URL</param-value>\\
     </init-param>\\
     <init-param>\\
       <param-name>serverName</param-name>\\
       <param-value>$SERVER_NAME</param-value>\\
     </init-param>\\
   </filter>\\
   <filter>\\
     <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>\\
     <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>\\
   </filter>\\
   <!-- /CAS -->\\
\\
   <filter>_" "$1"

sed -i "1,/<filter-mapping>/ s_<filter-mapping>_<!-- CAS -->\\
   <filter-mapping>\\
      <filter-name>CAS Authentication Filter</filter-name>\\
      <url-pattern>$FILTER_URL</url-pattern>\\
   </filter-mapping>\\
   <filter-mapping>\\
      <filter-name>CAS Validation Filter</filter-name>\\
      <url-pattern>$FILTER_URL</url-pattern>\\
   </filter-mapping>\\
   <filter-mapping>\\
      <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>\\
      <url-pattern>$FILTER_URL</url-pattern>\\
   </filter-mapping>\\
   <!-- /CAS -->\\
\\
   <filter-mapping>_" "$1"
